package cn.edu.hunau.java.week03.day03;

import cn.edu.hunau.java.week03.day02.Apple;
import org.junit.Test;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

public class AppleTest {

    @Test
    public void testApple(){
        Map<String,Map<String,Double>> map=parseApples(getApplies());
        System.out.println(map);
    }

    public Map<String, Map<String,Double>> parseApples(List<Apple> datas){
        Map<String, Map<String,Double>> map=new HashMap<>();
        Map<String,List<Double>> weightGroup=new HashMap<>();
        for(Apple apple:datas){
            String type=apple.getType();
            List<Double> weightList=weightGroup.getOrDefault(type,new ArrayList<>());
            weightList.add(apple.getWeight());
            weightGroup.put(type,weightList);
            }
        Set<Map.Entry<String, List<Double>>> entries = weightGroup.entrySet();
        for(Map.Entry<String, List<Double>> entry:entries){
            String type=entry.getKey();
            List<Double> weightList=entry.getValue();
            weightList.sort((a,b)->a.compareTo(b));
            Map<String,Double> countMap=new HashMap<>();
            countMap.put("min",weightList.getFirst());
            countMap.put("max",weightList.getLast());
            double sum=0;
            for(Double weight:weightList){
                sum+=weight;
            }
            double avg=sum/weightList.size();
            BigDecimal bd=new BigDecimal(avg);
            BigDecimal newBd=bd.setScale(2, RoundingMode.HALF_UP);
            countMap.put("avg",newBd.doubleValue());
            map.put(type,countMap);
        }
        System.out.println(weightGroup);
        return map;
    }

    public List<Apple> getApplies(){
        List<Apple> appleList=new ArrayList<>();
        // 添加50个Apple对象
        appleList.add(new Apple("红富士", "红色", 112.7));
        appleList.add(new Apple("红富士", "红色", 98.5));
        appleList.add(new Apple("红富士", "红色", 156.3));
        appleList.add(new Apple("红富士", "红色", 189.1));
        appleList.add(new Apple("红富士", "红色", 132.4));
        appleList.add(new Apple("红富士", "绿色", 105.6));
        appleList.add(new Apple("红富士", "绿色", 176.2));
        appleList.add(new Apple("红富士", "绿色", 89.3));
        appleList.add(new Apple("红富士", "绿色", 145.8));
        appleList.add(new Apple("红富士", "青色", 167.9));
        appleList.add(new Apple("红富士", "青色", 122.5));
        appleList.add(new Apple("红富士", "青色", 93.7));
        appleList.add(new Apple("红富士", "青色", 192.3));
        appleList.add(new Apple("瑞雪", "红色", 101.2));
        appleList.add(new Apple("瑞雪", "红色", 155.8));
        appleList.add(new Apple("瑞雪", "红色", 138.4));
        appleList.add(new Apple("瑞雪", "红色", 85.6));
        appleList.add(new Apple("瑞雪", "绿色", 177.3));
        appleList.add(new Apple("瑞雪", "绿色", 112.9));
        appleList.add(new Apple("瑞雪", "绿色", 143.2));
        appleList.add(new Apple("瑞雪", "绿色", 97.8));
        appleList.add(new Apple("瑞雪", "青色", 188.5));
        appleList.add(new Apple("瑞雪", "青色", 126.4));
        appleList.add(new Apple("瑞雪", "青色", 109.1));
        appleList.add(new Apple("瑞雪", "青色", 163.7));
        appleList.add(new Apple("红星", "红色", 92.4));
        appleList.add(new Apple("红星", "红色", 151.6));
        appleList.add(new Apple("红星", "红色", 128.3));
        appleList.add(new Apple("红星", "红色", 179.8));
        appleList.add(new Apple("红星", "绿色", 103.5));
        appleList.add(new Apple("红星", "绿色", 147.2));
        appleList.add(new Apple("红星", "绿色", 88.7));
        appleList.add(new Apple("红星", "绿色", 166.9));
        appleList.add(new Apple("红星", "青色", 133.1));
        appleList.add(new Apple("红星", "青色", 115.4));
        appleList.add(new Apple("红星", "青色", 191.2));
        appleList.add(new Apple("红星", "青色", 99.6));
        appleList.add(new Apple("国光", "红色", 108.3));
        appleList.add(new Apple("国光", "红色", 159.7));
        appleList.add(new Apple("国光", "红色", 136.2));
        appleList.add(new Apple("国光", "红色", 87.5));
        appleList.add(new Apple("国光", "绿色", 172.4));
        appleList.add(new Apple("国光", "绿色", 121.8));
        appleList.add(new Apple("国光", "绿色", 149.3));
        appleList.add(new Apple("国光", "绿色", 95.9));
        appleList.add(new Apple("国光", "青色", 183.6));
        appleList.add(new Apple("国光", "青色", 118.7));
        appleList.add(new Apple("国光", "青色", 164.2));
        appleList.add(new Apple("国光", "青色", 104.5));

        return appleList;
    }
}
